package com.yochi.pictureseditor.tools;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.net.Uri;
import android.os.Parcelable;
import android.provider.MediaStore;

import com.yochi.pictureseditor.R;
import com.yochi.pictureseditor.activities.SettingsChooser;
import com.yochi.pictureseditor.factories.ImageFactory;
import com.yochi.pictureseditor.images.Image;
import com.yochi.pictureseditor.libraries.cropimage.CropImage;

public class IntentUtils {
	
	public static final int INTENT_IMAGE_PICK = 100;
	public static final int INTENT_WALLPAPER = 101;
	public static final int INTENT_SETTINGS = 102;
	public static final int INTENT_CROP = 103;
	
	public static void loadImage(Activity context, Uri imageUri) {
		// camera intent :
		final List<Intent> cameraIntents = new ArrayList<Intent>();
		final Intent captureIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
		final PackageManager packageManager = context.getPackageManager();
		final List<ResolveInfo> listCam = packageManager.queryIntentActivities(captureIntent, 0);
		for(ResolveInfo res : listCam) {
			final String packageName = res.activityInfo.packageName;
			final Intent intent = new Intent(captureIntent);
			intent.setComponent(new ComponentName(res.activityInfo.packageName, res.activityInfo.name));
			intent.setPackage(packageName);
			intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
			cameraIntents.add(intent);
		}
		// filesystem :
		final Intent galleryIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
		// chooser of filesystem options intent :
		final Intent chooserIntent = Intent.createChooser(galleryIntent, context.getResources().getString(R.string.select_source));
		// add the camera options :
		chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, cameraIntents.toArray(new Parcelable[]{}));
		// start activity for result :
		context.startActivityForResult(chooserIntent, INTENT_IMAGE_PICK);
	}
	
	public static void setAs(Activity context, Image image) {
		String path = ImageFactory.saveImage(image);
		Uri uri = UriUtils.getUriForPath(path);
		Intent intent = new Intent(Intent.ACTION_ATTACH_DATA);
		intent.addCategory(Intent.CATEGORY_DEFAULT);
		intent.setDataAndType(uri, "image/png");
		intent.putExtra("mimeType", "image/png");
		context.startActivityForResult(Intent.createChooser(intent, context.getString(R.string.set_wallpaper)), INTENT_WALLPAPER);
	}
	
	public static void openSettings(Activity context) {
		Intent i = new Intent(context, SettingsChooser.class);
		context.startActivityForResult(i, INTENT_SETTINGS);
	}
	
	public static void cropRectangle(Activity context) {
		Intent intent = new Intent(context, CropImage.class);
        intent.putExtra(CropImage.SCALE, true);
        intent.putExtra(CropImage.CIRCLE_CROP, false);
        intent.putExtra(CropImage.ASPECT_X, 0);
        intent.putExtra(CropImage.ASPECT_Y, 0);
        context.startActivityForResult(intent, INTENT_CROP);
	}
	
	public static void cropCircle(Activity context) {
        Intent intent = new Intent(context, CropImage.class);
        intent.putExtra(CropImage.SCALE, true);
        intent.putExtra(CropImage.CIRCLE_CROP, true);
        intent.putExtra(CropImage.ASPECT_X, 2);
        intent.putExtra(CropImage.ASPECT_Y, 2);
        context.startActivityForResult(intent, IntentUtils.INTENT_CROP);
	}

}
